<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Law;
use App\Model\LawDetail;
use App\Model\LawCategory;
use App\Model\Checkcategory;
use Auth;

class LawController extends Controller
{
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            $user = Auth::user();
            if ($user['account']!=='ccshe') {
                echo "<script>alert('您没有权限!');</script>";exit;
            } 
            return $next($request);
        });
    }

    /*
    *法律法规添加页面
    *return html页面
    */
    public function add()
    {
        /*获取法律法规分类*/
        $law_category = LawCategory::get();

        /*获取九小场所分类*/
        $nine_category = Checkcategory::get();

        return view('Admin/Law/add', [
            'law_category' => $law_category,
            'nine_category' => $nine_category
        ]);
    }


    /*
    *法律法规添加内容保存
    *param array
    */
    public function addSave(Request $request)
    {
        $data = $request->all();
        
        $file = $this->uploadFile($request);

        $law = array();
        $law_detail = array();
        $law['title'] = $data['title'];
        $law['categoryId'] = $data['categoryId'];
        $law['execution'] = $data['execution'];
        $law['publishTime'] = $data['publishTime'];
        $law['checkCategoryId'] = $data['checkCategoryId'];
        $law['downUrl'] = $file['url'];
        $law['fileName'] = $file['name'];
        $law_detail['content'] = htmlentities($data['content']);

        $user = Auth::user();

        $law['addTime'] = time();
        $law['updateTime'] = time();
        $law['userId'] = $user['id'];

        $addRes = Law::insertGetId($law);
        if($addRes) {
            $law_detail['id'] = $addRes;
            $addDetailRes = LawDetail::insertGetId($law_detail);
        }

        return redirect(route('law_list'));

    }

    /*
    *法律法规列表
    *return array 法律法规列表
    */
    public function list()
    {
        $laws = Law::orderBy('updateTime','DESC')->get();

        return view('Admin/Law/list', array(
            'laws' => $laws
        ));
    }

    /*
    *更新法律法规内容
    *param int id  法律法规id
    *return array  法律法规内容
    */
    public function update(Request $request)
    {
        $id = $request->input('id');
        if(empty($id)) {
            \Session::flash('error', '参数错误!');
            return back();
        }
        $law = Law::where('id', $id)->first();
        if(empty($law)) {
            \Session::flash('error', '法律法规不存在!');
            return back();
        }
        $law_detail = LawDetail::where('id', $id)->first();
        $law_detail['content'] = html_entity_decode($law_detail['content'], 1);

        /*获取法律法规分类*/
        $law_category = LawCategory::get();
        /*获取九小场所分类*/
        $nine_category = Checkcategory::get();

        return view('Admin/Law/update', array(
            'law' => $law,
            'law_detail' => $law_detail,
            'law_category' => $law_category,
            'nine_category' => $nine_category
        ));
    }

    /*
    *更新法律法规内容保存
    *param array 文章内容
    */
    public function updateSave(Request $request)
    {
        $data = $request->all();

        $is_file = $request->file('word');
        $file = array();
        if($is_file) {
           $file = $this->uploadFile($request); 
        }

        $law = array();
        $law_detail = array();
        $law['title'] = $data['title'];
        $law['categoryId'] = $data['categoryId'];
        $law['execution'] = $data['execution'];
        $law['publishTime'] = $data['publishTime'];
        $law['checkCategoryId'] = $data['checkCategoryId'];
        if(!empty($file)) {
            $law['downUrl'] = $file['url'];
            $law['fileName'] = $file['name'];
        }
        $law['updateTime'] = time();
        $law_detail['content'] = htmlentities($data['content']);
        
        $law['updateTime'] = time();
        $updateRes = Law::where('id', $data['id'])->update($law);
        if($updateRes) {
            $updateDetailRes = LawDetail::where('id', $data['id'])->update($law_detail);
        }

        return redirect(route('law_list'));

    }

    protected function uploadFile(Request $request)
    {
        $file = $request->file('word');
        if($file->isValid()) {
            //$ext = $file->getClientOriginalExtension();
            //$path = $file->getRealPath();
            $filename = $file->getClientOriginalName();

            $filePosition = 'law/'.date('Ymd');
            $returnUrl = '/storage/'.$filePosition.'/'.$filename;

            $file->storePubliclyAs($filePosition, $filename, ['disk' => 'public']);

            return array('status'=>'success','url'=>$returnUrl,'name'=>$filename);
        } else {
            return array('status'=>'fail');
        }
        

    }
}
